<?php
/*
Plugin Name: dKret Eventcalendar Widget
Description: An additional Eventcalendar Widget for dKret (needs Eventcalendar plugin to show up).
Author: J. Kretzschmar
Version: 1.0
Author URI: http://diekretzschmars.de
*/

if (!function_exists('ec3_get_events'))
	return;

function widget_dkret_events($args) {
	extract($args);
	$options = get_option('widget_dkret_events');
	$title = empty($options['title']) ? __('Next Events','dkret') : $options['title'];
	echo "\t\t\t$before_widget\n";
	echo "\t\t\t\t$before_title".$title.$after_title;
ec3_get_events($options['events'],'<a href="%LINK%">%TITLE%</a>','%DATE%','','');
	echo "\n\t\t\t$after_widget";
}

// Admin section
function widget_control_dkret_events() {
 	global $wpdb;
 	$options = get_option('widget_dkret_events');
    		
	if ( $_POST['dkret-submit'] ) {

		$options['title'] = strip_tags(stripslashes($_POST['dkret-event-title']));
		$options['events'] = trim($_POST['dkret-events']);
		update_option('widget_dkret_events', $options);
	}
	
	$title = htmlspecialchars($options['title'], ENT_QUOTES);
	$events = $options['events'];

	// The Box content
	echo '<p style="text-align:right;"><label for="dkret-event-title" />' . __('Title:') . ' <input style="width: 200px;" id="dkret-event-title" name="dkret-event-title" type="text" value="'.$title.'" /></p>';
  echo '<p style="text-align:right;"><label for="dkret-events" />' . __('Number of Events:', 'dkret') . ' <input size="2" id="dkret-events" name="dkret-events" type="text" value="'.$events.'" /></p>';
  echo '<input type="hidden" id="dkret-submit" name="dkret-submit" value="1" />';
}

$options = get_option('widget_dkret_events');
if ( !is_array($options) )
	$options = array('events'=>'4',);
	update_option('widget_dkret_events', $options);

$widget_ops = array('classname' => 'widget_dkret_events', 'description' => __( "Your next event calendar events (dkret)", 'dkret') );
wp_register_sidebar_widget('events', __('Events','dkret'), 'widget_dkret_events', $widget_ops);
wp_register_widget_control('events', __('Events','dkret'), 'widget_control_dkret_events');
?>
